import { getAsyncComponent } from "@/utils/asyncComponentLoader";

// 异步加载组件
const DailyWaterUsage = getAsyncComponent(() =>
    import("@/components/availableFunctionList/DailyWaterUsageChart.vue")
);

const MonthlyElectricityChart = getAsyncComponent(() =>
    import("@/components/availableFunctionList/MonthlyElectricityChart.vue")
);
const StaticWaterTable = getAsyncComponent(() =>
    import("@/components/availableFunctionList/StaticWaterTable.vue")
);

const AreaWaterUsage = getAsyncComponent(() =>
    import("@/components/availableFunctionList/AreaWaterUsagePie.vue")
);
const AreaElectricityUsage = getAsyncComponent(() =>
    import("@/components/availableFunctionList/AreaElectricityUsagePie.vue")
);
const AreaElectricityTrend = getAsyncComponent(() =>
    import("@/components/availableFunctionList/AreaElectricityTrend.vue")
);
const AreaWaterTrend = getAsyncComponent(() =>
    import("@/components/availableFunctionList/AreaWaterTrend.vue")
);
const SubcategoryWaterPie = getAsyncComponent(() =>
    import("@/components/availableFunctionList/SubcategoryWaterPie.vue")
);
const SubcategoryWaterTrend = getAsyncComponent(() =>
    import("@/components/availableFunctionList/SubcategoryWaterTrend.vue")
);
const SubcategoryElectricityPie = getAsyncComponent(() =>
    import("@/components/availableFunctionList/SubcategoryElectricityPie.vue")
);
const SubcategoryElectricityTrend = getAsyncComponent(() =>
    import("@/components/availableFunctionList/SubcategoryElectricityTrend.vue")
);
const AreaWaterRanking = getAsyncComponent(() =>
    import("@/components/availableFunctionList/AreaWaterRanking.vue")
);
const AreaElectricityRanking = getAsyncComponent(() =>
    import("@/components/availableFunctionList/AreaElectricityRanking.vue")
);
const SubcategoryWaterRanking = getAsyncComponent(() =>
    import("@/components/availableFunctionList/SubcategoryWaterRanking.vue")
);
const SubcategoryElectricityRanking = getAsyncComponent(() =>
    import("@/components/availableFunctionList/SubcategoryElectricityRanking.vue")
);

// 组件映射
export const componentMap = {
    DailyWaterUsage,
    MonthlyElectricityChart,
    StaticWaterTable,
    AreaWaterUsage,
    AreaElectricityUsage,
    AreaElectricityTrend,
    AreaWaterTrend,
    SubcategoryWaterPie,
    SubcategoryWaterTrend,
    SubcategoryElectricityPie,
    SubcategoryElectricityTrend,
    AreaWaterRanking,
    AreaElectricityRanking,
    SubcategoryWaterRanking,
    SubcategoryElectricityRanking,
};

// 统一的卡片配置，包含展示信息和权限要求
export const dashboardCards = [
    {
        name: "30天用水趋势",
        component: "DailyWaterUsage",
        icon: "fa fa-line-chart",
        description: "展示30天每日用水量变化趋势",
        permissions: ['view_water_chart']
    },
    {
        name: "各月份用电量",
        component: "MonthlyElectricityChart",
        icon: "fa fa-bar-chart",
        description: "展示每个月用电量统计",
        permissions: ['view_electricity_chart']
    },
    {
        name: "漏损静态分析",
        component: "StaticWaterTable",
        icon: "fa fa-table",
        description: "展示漏损静态分析统计表格",
        permissions: ['view_leakage']
    },

    {
        name: "区域用水量",
        component: "AreaWaterUsage",
        icon: "fa fa-pie-chart",
        description: "展示年度区域用水量统计图",
        permissions: ['view_water_chart']
    },
    {
        name: "区域用电量",
        component: "AreaElectricityUsage",
        icon: "fa fa-pie-chart",
        description: "展示区域用电量分布统计图",
        permissions: ['view_electricity_chart']
    },
    {
        name: "区域用电趋势",
        component: "AreaElectricityTrend",
        icon: "fa fa-line-chart",
        description: "展示区域用电量变化趋势",
        permissions: ['view_electricity_chart']
    },
    {
        name: "区域用水趋势",
        component: "AreaWaterTrend",
        icon: "fa fa-line-chart",
        description: "展示区域用水量变化趋势",
        permissions: ['view_water_chart']
    },
    {
        name: "用途用水量",
        component: "SubcategoryWaterPie",
        icon: "fa fa-pie-chart",
        description: "展示用途用水量分布",
        permissions: ['view_water_chart']
    },
    {
        name: "用途用水趋势",
        component: "SubcategoryWaterTrend",
        icon: "fa fa-line-chart",
        description: "展示用途用水量变化趋势",
        permissions: ['view_water_chart']
    },
    {
        name: "用途用电量",
        component: "SubcategoryElectricityPie",
        icon: "fa fa-pie-chart",
        description: "展示用途用电量分布",
        permissions: ['view_electricity_chart']
    },
    {
        name: "用途用电趋势",
        component: "SubcategoryElectricityTrend",
        icon: "fa fa-line-chart",
        description: "展示用途用电量变化趋势",
        permissions: ['view_electricity_chart']
    },
    {
        name: "区域用水排名",
        component: "AreaWaterRanking",
        icon: "fa fa-sort-amount-desc",
        description: "展示区域用水量排名情况",
        permissions: ['view_water_chart']
    },
    {
        name: "区域用电排名",
        component: "AreaElectricityRanking",
        icon: "fa fa-sort-amount-desc",
        description: "展示区域用电量排名情况",
        permissions: ['view_electricity_chart']
    },
    {
        name: "用途用水排名",
        component: "SubcategoryWaterRanking",
        icon: "fa fa-sort-amount-desc",
        description: "展示用途用水量排名情况",
        permissions: ['view_water_chart']
    },
    {
        name: "用途用电排名",
        component: "SubcategoryElectricityRanking",
        icon: "fa fa-sort-amount-desc",
        description: "展示用途用电量排名情况",
        permissions: ['view_electricity_chart']
    }
];

// 检查用户是否拥有卡片所需权限的帮助函数
export function hasCardPermission(cardComponent, userPermissions) {
    const card = dashboardCards.find(card => card.component === cardComponent);
    if (!card || !card.permissions.length) return true; // 如果没有找到卡片或没有权限要求，默认允许访问
    return card.permissions.some(permission => userPermissions.includes(permission));
}

// 基于用户权限过滤可用卡片
export function filterAvailableCards(userPermissions) {
    return dashboardCards.filter(card =>
        card.permissions.some(permission => userPermissions.includes(permission))
    );
}

// 获取默认布局配置
export function getDefaultLayout() {
    // 桌面端默认布局
    const baseDesktopLayout = [
        { x: 0, y: 0, w: 1, h: 1, i: "0", component: "PerCapitaEnergy" },
        { x: 1, y: 0, w: 2, h: 1, i: "1", component: "DailyWaterUsage" },
        { x: 0, y: 1, w: 2, h: 1, i: "2", component: "MonthlyElectricityChart" },
        { x: 2, y: 1, w: 1, h: 1, i: "3", component: "StaticWaterTable" },
        { x: 0, y: 2, w: 1, h: 1, i: "4", component: "PerAreaEnergy" },
        { x: 1, y: 2, w: 1, h: 1, i: "5", component: "AreaWaterUsage" },
        { x: 2, y: 2, w: 1, h: 1, i: "6", component: "EnergyQuotaAchievement" }
    ];
    return baseDesktopLayout;
}